package com.ciko.code.logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciko.code.logic.bean.AddressBookInfo;
import com.ciko.code.logic.db.ContactsDBHelper;
import com.ciko.code.logic.table.ApproverBookInfoTable;

import java.util.ArrayList;
import java.util.List;


/**
 * 联系人 数据操作类
 */
public class ApproverBookInfoDao {
    private ContactsDBHelper dbHelper;

    private ApproverBookInfoDao(Context context) {
        dbHelper = ContactsDBHelper.getInstance(context);
    }

    public static ApproverBookInfoDao getInstance(Context context) {
        return new ApproverBookInfoDao(context);
    }

    public boolean add(AddressBookInfo bean) {
        try {
            final ContentValues values = dbHelper.getContentValues(ContactsDBHelper.TYPE_INSERT, bean);
            dbHelper.getSqliteDatabaseW().insert(ApproverBookInfoTable.table, null, values);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return false;
    }

    public boolean update(AddressBookInfo bean) {
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + ApproverBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                bean.getContactId(),
                String.valueOf(bean.getGroupId())
        };
        int v = 0;
        try {
            ContentValues values = dbHelper.getContentValues(ContactsDBHelper.TYPE_UPDATE, bean);
            v = dbHelper.getSqliteDatabaseW().update(ApproverBookInfoTable.table, values, selection, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return v > 0;
    }

    public boolean delete(String contactId, int groupId) {
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + ApproverBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                contactId,
                String.valueOf(groupId)
        };
        int v = dbHelper.getSqliteDatabaseW().delete(ApproverBookInfoTable.table, selection, args);
        dbHelper.closeSqliteDatabaseW();
        return v > 0;
    }

    public int deleteAll() {
        int count = dbHelper.getSqliteDatabaseW().delete(ApproverBookInfoTable.table, null, null);
        dbHelper.closeSqliteDatabaseW();
        return count;
    }

    public AddressBookInfo getById(String contactId, int groupId) {
        AddressBookInfo bean = null;
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.CONTACT_ID + " = ? and " + ApproverBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                contactId,
                String.valueOf(groupId)
        };
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                bean = dbHelper.getObject(AddressBookInfo.class, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return bean;
    }

    public AddressBookInfo getById(String contactId) {
        AddressBookInfo bean = null;
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.ACC_ID + " = ? ";
        final String[] args = new String[]{
                contactId
        };
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                bean = dbHelper.getObject(AddressBookInfo.class, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return bean;
    }

    public List<AddressBookInfo> getListByGroupId(int groupId) {
        List<AddressBookInfo> list = null;
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.GROUP_ID + " = ? ";
        final String[] args = new String[]{
                String.valueOf(groupId)
        };
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                list = new ArrayList<>();
                while (c.moveToNext()) {
                    list.add(dbHelper.getObject(AddressBookInfo.class, c));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return list;
    }

    public List<AddressBookInfo> getAll() {
        List<AddressBookInfo> list = null;
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, null, null, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                list = new ArrayList<>();
                while (c.moveToNext()) {
                    list.add(dbHelper.getObject(AddressBookInfo.class, c));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }
        return list;
    }

    public List<AddressBookInfo> search(String string) {
        List<AddressBookInfo> list = null;
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.CONTACT_NAME + " like '%" + string + "%'";
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, selection, null, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                list = new ArrayList<>();
                while (c.moveToNext()) {
                    list.add(dbHelper.getObject(AddressBookInfo.class, c));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return list;
    }
    public List<AddressBookInfo> searchByPhone(String string) {
        List<AddressBookInfo> list = null;
        final String selection = ApproverBookInfoTable.AddressBookInfoColums.MOBILE_NUMBER + " like '%" + string + "%'";
        Cursor c = dbHelper.getSqliteDatabaseR().query(ApproverBookInfoTable.table, null, selection, null, null, null, null);
        try
        {
            if (c != null && c.getCount() > 0)
            {
                list = new ArrayList<>();
                while (c.moveToNext())
                {
                    list.add(dbHelper.getObject(AddressBookInfo.class, c));
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }

        return list;
    }
}
